package com.fh.modules.dbms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.common.util.StringUtils;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.dbms.entity.DbmsTableIndex;
import com.fh.modules.dbms.mapper.DbmsTableIndexMapper;
import com.fh.modules.dbms.service.IDbmsTableIndexColumnService;
import com.fh.modules.dbms.service.IDbmsTableIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author qiujunda
 * @Date 23/07/2020
 * @Description
 */
@Service
public class DbmsTableIndexServiceImpl extends AbstractServiceImpl<DbmsTableIndexMapper, DbmsTableIndex> implements IDbmsTableIndexService {

    @Autowired
    private IDbmsTableIndexColumnService dbmsTableIndexColumnService;

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean deleteByTableId(Long tableId){
        List<DbmsTableIndex> dbmsTableIndexList=this.queryDbmsTableIndex(tableId);
        for(DbmsTableIndex dbmsTableIndex:dbmsTableIndexList){
            dbmsTableIndexColumnService.deleteByTableId(dbmsTableIndex.getId());
            dbmsTableIndex.setDelFlag(1);
            this.updateById(dbmsTableIndex);
        }
        return true;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public boolean save(DbmsTableIndex entity) {
        return super.save(entity);
    }

    @Override
    public List<DbmsTableIndex> queryDbmsTableIndex(Long tableId){
        QueryWrapper<DbmsTableIndex> queryWrapper = new QueryWrapper<DbmsTableIndex>();
        queryWrapper.eq("table_id", tableId).eq("del_flag",0);
        queryWrapper.orderByAsc("index_name");
        return this.list(queryWrapper);
    }

    @Override
    public boolean compareTableIndex(Long tableId1, Long tableId2){
        List<DbmsTableIndex> list1=this.queryDbmsTableIndex(tableId1);
        List<DbmsTableIndex> list2=this.queryDbmsTableIndex(tableId2);
        if(list1.size()==list2.size()){
            int length=list1.size();
            for(int i=0;i<length;i++){
                DbmsTableIndex dbmsTableIndex1=list1.get(i);
                DbmsTableIndex dbmsTableIndex2=list2.get(i);
                if(!StringUtils.compareStr(dbmsTableIndex1.getIndexName(),dbmsTableIndex2.getIndexName())||
                !StringUtils.compareStr(dbmsTableIndex1.getIndexType(),dbmsTableIndex2.getIndexType())||
                !StringUtils.comareInteger(dbmsTableIndex1.getColumnNum(),dbmsTableIndex2.getColumnNum())){
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public List<DbmsTableIndex> queryTableIndexs(Long tableId){
        QueryWrapper<DbmsTableIndex> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", tableId).eq("del_flag",0);
        queryWrapper.orderByAsc("index_name");
        return this.list(queryWrapper);
    }

}
